#coding:utf-8
'''
create on 2017-06-13
@author:sandy
'''

from pycommon import TBaseFactory
import Client
import time
import logging

class ClientFactory(TBaseFactory.TBaseFactory):

    def __init__(self):
        TBaseFactory.TBaseFactory.__init__(self)

    def createClient(self, pid, addr):
        return Client.ClientSrv(pid, addr)

    def checkClients(self, clients):
        curtime = time.time()
        for c in clients:
            c.sendKeepAlive()
            if c.getState() == Client.CLIENT_STATE_TO_CLOSE and curtime - c.toclosetime > 10:
                c.abort()
            elif curtime-c.getLastActiveTime() > 60:
                c.abort()      


    def removeProtocol(self, prot):
        TBaseFactory.TBaseFactory.removeProtocol(self, prot)

    def getClientSrvBySid(self, sid):
        clients = self.getClients()
        for c in clients:
            if c.getSId() == sid:
                return c
        return None

instance = ClientFactory()

if __name__ == "__main__":
    pass